from fastapi import APIRouter, Depends
from fastapi.security import OAuth2PasswordRequestForm

from dependencies import create_access_token, get_captcha_code
from models import User
from schemas import FailResp, SuccessResp, Token

router = APIRouter(prefix="/auth", tags=["开发调试 - 认证"])


@router.get("/code", summary="查看图片验证码")
async def code(code_in_redis: str = Depends(get_captcha_code)):
    data = {"code": code_in_redis}
    return SuccessResp(data=data)


@router.post("/token", summary="获取 token")
async def token(form_data: OAuth2PasswordRequestForm = Depends()):
    user = await User.get_or_none(username=form_data.username)
    if user is None:
        return FailResp(code=10201, msg="用户名与密码不匹配")
    if not user.check_password(form_data.password):
        return FailResp(code=10201, msg="用户名与密码不匹配")
    access_token = create_access_token(data={"sub": user.username})
    token = Token(access_token=access_token, token_type="bearer")
    data = token.model_dump(by_alias=False)
    return data
